﻿@model Retete.ViewModels.MeniuViewModel
@{
    ViewBag.Title = "Meniuri";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    window.onload = function () {
        if (FocusRetetaNoua.value == "True") RetetaNouaNume.focus();
        else MeniuNouCod.focus();
    }

    function submitOnEnter() {
        if (event.keyCode == "13") form.submit();
    } 
    function stergeInregistrare(id) {
        if (confirm("Doriti sa stergeti reteta selectata?") == true) {
            IDRetetaStearsa.value = id;
            form.submit();
        }
    }
    function clonareMeniu() {
        if (confirm("Doriti sa clonati meniul?") == true) {
            ClonareMeniu.value = true;
            form.submit();
        }
    }
</script>

<script type="text/javascript">
    $(function () {
        $("#RetetaNouaNume").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/Retete")',
                    dataType: "json",
                    data: { maxRows: 8, contains: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.Cod + ' ' + item.Nume, nume: item.Nume, cod: item.Cod, id: item.ID, pret: item.Pret }
                        }));
                    }
                });
            },
            minLength: 2,
            delay: 50,
            select: function (event, ui) {
                RetetaNouaPret.innerText = ui.item.pret;
                RetetaNouaID.value = ui.item.id;
                RetetaNouaNume.value = ui.item.nume;
                RetetaNouaCod.innerText = ui.item.cod;
            }
        });
    });

    $(function () {
        $("#Meniu_Valabilitate").datepicker();
    });
</script>

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form", name = "form" }))
{
    <table style="width: 100%">
        <tr>
            <td style="width: 190px; vertical-align: top; border-right-style: dashed; border-right-color: #CFCFCF; border-right-width: 1px;">
                <fieldset>
                    <legend>Activitati</legend>

                    <p class="toolbox"><a href="javascript:form.submit()">Accepta</a></p>
                    <p class="toolbox">@Html.ActionLink("Renunta", "Index")</p>
                    <br />
                    <p class="toolbox"><a href="javascript:clonareMeniu()">Cloneaza meniu</a></p>
                </fieldset>
            </td>
            <td style="vertical-align: top;">
                @Html.ValidationSummary(false)
                @Html.HiddenFor(model => model.IDRetetaStearsa)
                @Html.HiddenFor(model => model.FocusRetetaNoua)
                @Html.HiddenFor(model => model.ClonareMeniu)

                <fieldset>
                    <legend>Modificare meniu</legend>

                    <p>
                        @Html.LabelFor(model => model.Meniu.Cod, "Cod")
                        @Html.TextBoxFor(model => model.Meniu.Cod)
                        @Html.HiddenFor(model => model.Meniu.ID)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Meniu.Denumire, "Denumire")
                        @Html.TextBoxFor(model => model.Meniu.Denumire)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Meniu.Pret, "Pret")
                        @Html.EditorFor(model => model.Meniu.Pret)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Meniu.Valabilitate, "Valabilitate")
                        @Html.TextBoxFor(model => model.Meniu.Valabilitate)
                    </p>
                </fieldset>
    
                <fieldset>
                    <legend>Retete incluse in meniu</legend>
            
                    <table class="grid">
                        <thead>
                            <tr>
                                <th style="width: 16px"></th>
                                <th style="width: 70px">Cod</th>
                                <th>Reteta</th>
                                <th style="width: 110px">Pret</th>
                                <th style="width: 110px">Portii</th>
                            </tr>
                        </thead>
        
                        <tbody id="tableBody">
                            <tr class="insertionRow">
                                <td></td>
                                <td id="RetetaNouaCod"></td>
                                <td>
                                    @Html.TextBoxFor(model => model.RetetaNouaNume, new { autocomplete = "off" })
                                    @Html.HiddenFor(model => model.RetetaNouaID)
                                </td>
                                <td id="RetetaNouaPret"></td>
                                <td>@Html.TextBoxFor(model => model.RetetaNouaPortii, new { autocomplete = "off", onkeydown = "return submitOnEnter()" })</td>
                            </tr>

                            @if (Model.Retete != null)
                            {
                                for (int i = 0; i < Model.Retete.Count; i++)
                                {
                                    <tr>
                                        <td>
                                            <img src="@Url.Content("~/Content/Images/Delete.png")" alt="Sterge" style="height: 12px; cursor: pointer;" onclick="stergeInregistrare(@Model.Retete[i].ID)" />
                                            @Html.HiddenFor(element => element.Retete[i].ID)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Retete[i].Reteta.Cod)
                                            @Html.HiddenFor(element => element.Retete[i].Reteta.Cod)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Retete[i].Reteta.Nume)
                                            @Html.HiddenFor(element => element.Retete[i].Reteta.Nume)
                                            @Html.HiddenFor(element => element.Retete[i].IDReteta)
                                        </td>
                                        <td>
                                            @Model.Retete[i].Reteta.Pret.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
                                            @Html.HiddenFor(element => element.Retete[i].Reteta.Pret)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Retete[i].Portii)
                                            @Html.HiddenFor(element => element.Retete[i].Portii)
                                        </td>
                                    </tr>
                                }
                            }
                        </tbody>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
}